﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ZG_member.DAL
{
    public class CourseStage
    {
        public bool add(ZG_member.Model.CourseStage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CourseStage(");
            strSql.Append("StageName,StageContent)");
            strSql.Append("values (");
            strSql.Append("@StageName,@StageContent)");
            SqlParameter[] parameters = {
					 
					new SqlParameter("@StageName", SqlDbType.VarChar,50),
					new SqlParameter("@StageContent", SqlDbType.VarChar,5000)};

            parameters[0].Value = model.StageName;
            parameters[1].Value = model.StageContent;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public DataSet GetListWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder(); 
            strSql.Append("select * ");
            strSql.Append(" FROM CourseStage");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZG_member.Model.CourseStage mode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CourseStage set ");
            strSql.Append("StageName=@StageName, ");
            strSql.Append("StageContent=@StageContent");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
                                            new SqlParameter("@StageName", SqlDbType.VarChar,100),
                                            new SqlParameter("@StageContent", SqlDbType.VarChar),
                                            new SqlParameter("@Id", SqlDbType.Int)
					                        };
            parameters[0].Value = mode.StageName;
            parameters[1].Value = mode.StageContent;
            parameters[2].Value = mode.Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return DbHelperSQL.Query(strWhere.ToString());
        }


        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CourseStage ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public ZG_member.Model.CourseStage GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,StageName,StageContent from CourseStage ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            ZG_member.Model.CourseStage model = new ZG_member.Model.CourseStage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StageName"] != null && ds.Tables[0].Rows[0]["StageName"].ToString() != "")
                {
                    model.StageName = ds.Tables[0].Rows[0]["StageName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["StageContent"] != null && ds.Tables[0].Rows[0]["StageContent"].ToString() != "")
                {
                    model.StageContent = ds.Tables[0].Rows[0]["StageContent"].ToString();
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        public ZG_member.Model.CourseStage GetModel(string StageName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,StageName,StageContent from CourseStage ");
            strSql.Append(" where StageName=@StageName");
            SqlParameter[] parameters = {
					new SqlParameter("@StageName", SqlDbType.VarChar)
};
            parameters[0].Value = StageName;

            ZG_member.Model.CourseStage model = new ZG_member.Model.CourseStage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StageName"] != null && ds.Tables[0].Rows[0]["StageName"].ToString() != "")
                {
                    model.StageName = ds.Tables[0].Rows[0]["StageName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["StageContent"] != null && ds.Tables[0].Rows[0]["StageContent"].ToString() != "")
                {
                    model.StageContent = ds.Tables[0].Rows[0]["StageContent"].ToString();
                }

                return model;
            }
            else
            {
                return null;
            }
        }


    }
}
